clear all
global g_root "C:\Users\Hongye Guo\Dropbox (Personal)\Density\Code\Production\Submission"
local l_date : display %tdCYND date(c(current_date), "DMY")
local l_time = substr(c(current_time), 1, 2) + substr(c(current_time), 4, 2)

use "$g_root\Data\vwretd_clean_202106.dta", clear

sort ym
forvalues i = 1/12{
	gen vwretd_l`i'm = vwretd[_n-`i']
}
gen vwretd_f1m = vwretd[_n+1]

gen month = month(dofm(ym+1))
gen am1_flag = mod(month, 3) == 1
gen vwretd_t0_11m = vwretd + vwretd_l1m + vwretd_l2m + vwretd_l3m + vwretd_l4m + vwretd_l5m + vwretd_l6m + vwretd_l7m + vwretd_l8m + vwretd_l9m + vwretd_l10m + vwretd_l11m
gen vwretd_t0_11m_x_am1_flag = vwretd_t0_11m * am1_flag

gen vwretd_l0m = vwretd
forvalues i = 1/4{
	local j = (`i'-1)*3 
	local jp1 = `j' + 1
	local jp2 = `j' + 2
	foreach l_var in vwretd {
		gen `l_var'_prevq1_l`i'q = `l_var'_l`j'm
		replace `l_var'_prevq1_l`i'q = `l_var'_l`jp1'm if mod(month,3)==0
		replace `l_var'_prevq1_l`i'q = `l_var'_l`jp2'm if mod(month,3)==1
	}
}

forvalues ii = 1/4{
	foreach l_var in vwretd {
		gen `l_var'_prevq1_l`ii'q_x_am1 = am1_flag * `l_var'_prevq1_l`ii'q
	}	
}

foreach l_var in vwretd {
	gen `l_var'_prevq1_t4q = `l_var'_prevq1_l1q + `l_var'_prevq1_l2q + `l_var'_prevq1_l3q + `l_var'_prevq1_l4q 
	gen `l_var'_prevq1_t4q_x_am1_flag = `l_var'_prevq1_t4q * am1_flag					
}

* Table 2
local l_length = 4
local l_var_list 
local l_var_list_int 
forvalues ii = 1/`l_length'{
	local l_var_list `l_var_list' vwretd_prevq1_l`ii'q
}
forvalues ii = 1/`l_length'{
	local l_var_list_int `l_var_list_int' vwretd_prevq1_l`ii'q_x_am1
}

local col_count = 0
local title_list
reg vwretd_f1m `l_var_list', robust
local col_count = `col_count' + 1
estimates store exp_`col_count'	
local title_list `title_list' "All"			
reg vwretd_f1m `l_var_list' if am1_flag, robust
local col_count = `col_count' + 1
estimates store exp_`col_count'	
local title_list `title_list' "AM"			
reg vwretd_f1m `l_var_list' if !am1_flag, robust			
local col_count = `col_count' + 1
estimates store exp_`col_count'	
local title_list `title_list' "Non-AM"						
reg vwretd_f1m `l_var_list' `l_var_list_int' am1_flag, robust			
local col_count = `col_count' + 1
estimates store exp_`col_count'	
local title_list `title_list' "All"	

local order_list `l_var_list' `l_var_list_int' am1_flag
esttab exp_* using "$g_root/Output/USMomentum_exp1_`l_date'`l_time'.csv", star(* 0.1  ** 0.05 *** 0.01) b(3) t(2) r2 br noomit replace order(`order_list') mtitles(`title_list')

* Table 3 & OA.D1
local col_count = 0
local title_list
reg vwretd_f1m vwretd_prevq1_t4q , robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "All" 
reg vwretd_f1m vwretd_prevq1_t4q  if am1_flag, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "AM" 
reg vwretd_f1m vwretd_prevq1_t4q  if !am1_flag, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "Non-AM" 
reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag , robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "All" 

reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag if ym>=ym(1947, 1), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "Post-war" 
reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag if ym<ym(1974, 1), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "First Half" 
reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag if ym>=ym(1974, 1), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "Second Half" 

gen vwretd_no1_t4q = vwretd_t0_11m - vwretd_prevq1_t4q
gen vwretd_no1_t4qxam = vwretd_no1_t4q * am1_flag

reg vwretd_f1m vwretd_t0_11m , robust 
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "All" 
reg vwretd_f1m vwretd_t0_11m if am1_flag, robust 
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "AM" 
reg vwretd_f1m vwretd_t0_11m if !am1_flag, robust 
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "Non-AM" 
reg vwretd_f1m vwretd_t0_11m vwretd_t0_11m_x_am1_flag am1_flag, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "All" 
reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag vwretd_no1_t4q vwretd_no1_t4qxam am1_flag, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "All" 

local order_list vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag vwretd_t0_11m vwretd_t0_11m_x_am1_flag am1_flag
esttab sep_* using "$g_root/Output/USMomentum_sep1_`l_date'`l_time'.csv", star(* 0.1  ** 0.05 *** 0.01) b(3) t(2) r2 br noomit replace order(`order_list') mtitles(`title_list')

* Table 6
* Distinction on the first quarter	
clear matrix
local col_count = 0
local title_list
reg vwretd_f1m vwretd_prevq1_t4q if month == 1, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "Jan" 
reg vwretd_f1m vwretd_prevq1_t4q if month == 2, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "Feb" 
reg vwretd_f1m vwretd_prevq1_t4q if month == 3, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "Mar" 
reg vwretd_f1m vwretd_prevq1_t4q if mod(month, 3)==1 & month != 1, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "Group 1 no Jan" 
reg vwretd_f1m vwretd_prevq1_t4q if mod(month, 3)==2 & month != 2, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "Group 2 no Feb" 
reg vwretd_f1m vwretd_prevq1_t4q if mod(month, 3)==0 & month != 3, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local title_list `title_list' "Group 3 no Mar" 
esttab sep_* using "$g_root/Output/Q1_sep1_`l_date'`l_time'.csv", star(* 0.1  ** 0.05 *** 0.01) b(3) t(2) r2 br noomit replace 

* Table A1
** Generate split samples by decades
clear matrix
local col_count = 0
reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag ///
	if ym<ym(1940, 1), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	

reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag ///
	if ym>=ym(1940, 1) & ym<ym(1950, 1), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	

reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag ///
	if ym>=ym(1950, 1) & ym<ym(1960, 1), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	

reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag ///
	if ym>=ym(1960, 1) & ym<ym(1970, 1), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	

reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag ///
	if ym>=ym(1970, 1) & ym<ym(1980, 1), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	

reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag ///
	if ym>=ym(1980, 1) & ym<ym(1990, 1), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	

reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag ///
	if ym>=ym(1990, 1) & ym<ym(2000, 1), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	

reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag ///
	if ym>=ym(2000, 1) & ym<ym(2010, 1), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	

reg vwretd_f1m vwretd_prevq1_t4q vwretd_prevq1_t4q_x_am1_flag am1_flag ///
	if ym>=ym(2010, 1) , robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
local l_date : display %tdCYND date(c(current_date), "DMY")
local l_time = substr(c(current_time), 1, 2) + substr(c(current_time), 4, 2)
esttab sep_* using "$g_root/Output/Decade_sep1_`l_date'`l_time'.csv", star(* 0.1  ** 0.05 *** 0.01) b(3) t(2) r2 br noomit replace


